<?php

namespace App\Dao;

use App\Model\Anchor;
use App\Model\AnchorSortLog;
use Hyperf\Context\ApplicationContext;
use Hyperf\DbConnection\Db;

class AnchorSortLogDao
{
    private static $instance;

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = Applicationcontext::getcontainer()->get(self::class);
        }
        return self::$instance;
    }

    public static array $fromType = [
        // ['type'=>1,'name'=>'昨日在线时长','score'=>0.1],
        ['type' => 2, 'name' => '主叫接通', 'score' => 5],
        ['type' => 3, 'name' => '被叫接通', 'score' => 5],
        ['type' => 4, 'name' => '主叫速聊接通', 'score' => 3],
        ['type' => 5, 'name' => '发布动态', 'score' => 1],
        ['type' => 6, 'name' => '上传相册', 'score' => 1],
        ['type' => 7, 'name' => '上传视频', 'score' => 2],
        ['type' => 8, 'name' => '撩他', 'score' => 0.2],
        ['type' => 9, 'name' => '主动私信', 'score' => 0.1],
        ['type' => 10, 'name' => '私信回复', 'score' => 1],
        ['type' => 11, 'name' => '视频收益', 'score' => 0.01],
        ['type' => 12, 'name' => '礼物收益', 'score' => 0.05],
        // ['type'=>13,'name'=>'初始分值1','score'=>0.1],
        // ['type'=>14,'name'=>'初始分值2','score'=>0.1],
        ['type' => 15, 'name' => '被叫未接听', 'score' => -3],
        ['type' => 16, 'name' => '速聊未接听（被叫）', 'score' => -5],
        ['type' => 17, 'name' => '私信无效回复', 'score' => -2],
    ];


    /**
     * Note - 添加记录
     * DateTime 2024/7/22 15:05
     * User Admin
     * @param $uid
     * @param $from
     * @param $count
     * @param $remark
     * @return bool
     */
    public static function record($uid, $from, $count = 1, $remark = '')
    {

        $fromType = array_column(self::$fromType, null, 'type');
        if (!isset($fromType[$from]))
            return false;

        $score = $count * $fromType[$from]['score'];

        switch ($from) {
            case 2:
            case 4:
                // 主叫接通分数
                $field = "score_calling_num";
                break;
            case 3:
            case 15:
            case 16:
                // 被叫接通分数
                $field = "score_called_num";
                break;
            case 11:
            case 12:
                // 收益分
                $field = "score_income";
                break;
            case 8:
            case 9:
            case 10:
            case 17:
                // 私信回复分
                $field = "score_mes";
                break;
            default :
                $field = false;
        }

        if ($field !== false) {
            // $sql = "update tb_anchor set {$field}={$field}+{$score},score_sort=score_sort+{$score} where uid={$uid}";
            Anchor::query()->where('uid', $uid)->update([$field => Db::raw("$field+$score"), 'score_sort' => Db::raw("score_sort+$score")]);
        } else {
            // $sql = "update tb_anchor set score_sort=score_sort+{$score} where uid={$uid}";
            // $pd = (new self)->conn->prepare($sql);
            // $rs = $pd->execute();
            Anchor::query()->where('uid', $uid)->increment('score_sort', $score);
        }
        $recordData = [
            'uid' => $uid,
            'type' => $score > 0 ? 1 : 2,
            'from_type' => $from,
            'change' => $score,
            'remark' => $remark,
            'ctime' => time(),
        ];
        AnchorSortLog::query()->insert($recordData);
        return true;

    }


}